<?php
namespace app\admin\controller;
use think\view;
use think\Db;
use think\Controller;

class Lou extends CommonController
{
    public function _initialize()
    {
        $this->sql =  Db::name('home');
    }
    //管理
    public function index()
    {
        if($_GET['search']){
            $search =  $_GET['search'];

            if($search==null){
                $list = '';
            }else{

                $list = $this->sql
                    ->where('title','like','%'.$search.'%')
                    ->select();
                $this->assign('list',$list);

            }
        }else{
            $count = $this->sql->count(); //计算记录数
            $limitRows = 10; // 设置每页记录数
            $p = new AjaxPage($count, $limitRows,"news"); //第三个参数是你需要调用换页的ajax函数名

            $limit_value = $p->firstRow . "," . $p->listRows;
            $list = $this->sql->order('uptime desc')->limit($limit_value)->select();
            $page = $p->show(); // 产生分页信息，AJAX的连接在此处生成

            $this->assign('list',$list);

            $this->assign('page',$page);
        }
        if(!empty($res['p'])){
            $p = $res['p'];
            $this->assign('p',$p);

        }


        return $this->fetch();

    }

    //新增
    public  function  add(){
        if($_POST){
            $fileInfo = $_FILES['myFile'];
            $pic = $this->uploadFile($fileInfo);

            $_POST['pic'] = $pic;
            $_POST['uptime'] = date('Y-m-d H:i:s',time());
            $add = $this->sql->insert($_POST);
            if($add){
                $this->success('添加成功', url('lou/index'));
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    public function edit()
    {
        if($_GET){
            $map['id']  = $_GET['id'];
            $list = $this->sql->where($map)->find();
            $this->assign('list',$list);
        }
        if($_POST){
            $id  = $_POST['id'];
            $fileInfo = $_FILES['myFile'];
            if($fileInfo['error']==4){

                $_POST['uptime'] = date('Y-m-d H:i:s',time());
                $add =$this->sql->where(array(
                    'id'=>$id
                ))->update($_POST);
            }else{
                $pic = $this->uploadFile($fileInfo);
                $_POST['pic'] = $pic;
                $_POST['uptime'] = date('Y-m-d H:i:s',time());
                $add = $this->sql->where(array(
                    'id'=>$id
                ))->update($_POST);
            }

            if($add){
                $this->success('修改成功', url('lou/index'));

            }else{
                $this->error('修改失败');
            }

        }

        return $this->fetch();
    }

    //删除
    public  function del(){
        if($_POST){
            $map['id'] = $_POST['id'];
            $res = $this->sql->where($map)->delete();
            if($res){
                return show(1,'删除成功');
            }else{
                return show(0,'删除失败');
            }
        }

    }


    //轮播图

    public function lun()
    {
            $request = request();
            $res = $request->param();

            $list = DB::name('lun')->order('uptime desc')
                ->where(array(
                    'home_id'=>$res['id']
                ))
                ->select();
            $this->assign('list',$list);
            $this->assign('id',$res['id']);
           return $this->fetch();

    }

    public function add_lun(){
        $request = request();
        $res = $request->param();
        $this->assign('id',$res['id']);
        if($_POST){
            $fileInfo = $_FILES['myFile'];
            $pic = $this->uploadFile($fileInfo);

            $_POST['pic'] = $pic;
            $_POST['uptime'] = date('Y-m-d H:i:s',time());
            $add = Db::name('lun')->insert($_POST);
            if($add){
                $this->success('添加成功', url('lou/index'));
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    public function del_lun(){
        if($_POST){
            $map['id'] = $_POST['id'];
            $res = Db::name('lun')->where($map)->delete();
            if($res){
                return show(1,'删除成功');
            }else{
                return show(0,'删除失败');
            }
        }
    }

    //户型图
    public function hu()
    {
        $request = request();
        $res = $request->param();

        $list = DB::name('hu')->order('uptime desc')
            ->where(array(
                'home_id'=>$res['id']
            ))
            ->select();
        $this->assign('list',$list);
        $this->assign('id',$res['id']);
//        echo $res['id'];

        return $this->fetch();

    }
    public function add_hu(){
        $request = request();
        $res = $request->param();
        $this->assign('id',$res['id']);
        if($_POST){
            $fileInfo = $_FILES['myFile'];
            $pic = $this->uploadFile($fileInfo);

            $_POST['pic'] = $pic;
            $_POST['uptime'] = date('Y-m-d H:i:s',time());
            $add = Db::name('hu')->insert($_POST);
            if($add){
                $this->success('添加成功', url('lou/index'));
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    public function del_hu(){
        if($_POST){
            $map['id'] = $_POST['id'];
            $res = Db::name('hu')->where($map)->delete();
            if($res){
                return show(1,'删除成功');
            }else{
                return show(0,'删除失败');
            }
        }
    }

    /*
   * 设置排序
   */
    public function  order(){
        $map['id'] = $_POST['id'];

        $res = Db::name('lun')->where($map)->update($_POST);
        if($res){
            return show(1,'设置排序成功！');
        }else{
            return show(0,'设置排序失败！');
        }
    }



    private  function uploadFile($fileInfo,$allowExt = array('jpeg','jpg','gif','png'), $maxSize = 2097152000, $uploadPath = './Update',$flag = true)
    {

        if ($fileInfo['error'] > 0) {

            switch ($fileInfo['error']) {
                case 1:
                    $mes = 'The upload file exceeds the upload in the PHP configuration file_ max_ The value of the file size option';
                    break;
                case 2:
                    $mes = 'Exceeded form Max_ FILE_ Size limit size';
                    break;
                case 3:
                    $mes = 'Part of the file was uploaded';
                    break;

                case 6:
                    $mes = 'No temporary directory found';
                    break;
                case 7:
                case 8:
                    $mes = 'System error';
                    break;
            }
            exit($mes);
        }
        $ext = pathinfo($fileInfo['name'], PATHINFO_EXTENSION);
//    $allowExt = array('jpeg', 'jpg', 'png', 'git');

        if (!in_array($ext, $allowExt)) {
            exit('Illegal file type');
        }


        if ($fileInfo['size'] > $maxSize) {
            exit('The upload file is too large');
        }

        if($flag){
            if(!getimagesize($fileInfo['tmp_name'])){
                exit('It\'s not a real picture type');
            }
        }

        if (!is_uploaded_file($fileInfo['tmp_name'])) {
            exit('t\'s not a real image type file. It\'s not uploaded through HTTP post');
        }
//    $uploadPath = 'uploads';
        if (!file_exists($uploadPath)) {
            mkdir($uploadPath, 0777, true);
            chmod($uploadPath, 0777);
        }
        $uniName =substr( md5(uniqid(microtime(true), true)),1,5) . '.' . $ext;
        $destination = $uploadPath . '/' . $uniName;
        if (!@move_uploaded_file($fileInfo['tmp_name'], $destination)) {
            exit('File move failed');
        }

//    return array(
//        'newName'=>$destination,
//        'size'=>$fileInfo['size'],
//        'type'=>$fileInfo['type']
//    );
        return $destination;
    }

}
